import axios from "axios";
import {usePcKeyStore} from "@/stores/pckey.js";

export const readJsonFile = () => {
    const pcKeyStore = usePcKeyStore()
    axios.get(
        "/api/readJsonFile",
        {})
        .then(
            (res) => {
                const data = res.data
                if (data === null || data === "" || data === "文件不存在") {
                    ElMessage.error("文件不存在或文件为空")
                } else if (data === "未知错误") {
                    ElMessage.error("未知错误")
                } else {
                    ElMessage.success("读取成功")
                    pcKeyStore.pckey = data
                }
            }
        )
}

export const writeJsonFile = (json) => {
    axios.post(
        "/api/writeJsonFile",
        json)
        .then(
            (res) => {
                const data = res.data
                if (data === "未知错误") {
                    ElMessage.error("未知错误")
                } else {
                    ElMessage.success("写入成功")
                }
            }
        )
}

export const shutdown = () => {
    axios.post("/api/shutdown")
        .then(
            (res) => {
                const data = res.data
                if (data === "未知错误") {
                    ElMessage.error("未知错误")
                } else {
                    ElMessage.success(data)
                }
            }
        )
}